//
//  JZEClassPlayerController.m
//  eStudy(parents)
//
//  Created by 朱献国 on 2018/8/21.
//  Copyright © 2018年 苏州橘子网络科技股份有限公司. All rights reserved.
//

#import "JZEClassPlayerController.h"
#import "CLPlayerView.h"
#import <YYKit.h>

@interface JZEClassPlayerController ()
@property (nonatomic, strong) CLPlayerView * playerView;
@end

@implementation JZEClassPlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
  self.view.backgroundColor = [UIColor whiteColor];
  
    // player
    [self.view addSubview:self.playerView];
    
    self.playerView.url = [NSURL URLWithString:self.videoUrl];
  
//    @weakify(self);
    // 返回按钮被点击
    [self.playerView backButton:^(UIButton *button) {
//        @strongify(self);
        NSLog(@"返回按钮被点击");
        // 查询是否是全屏状态
//        if (self.playerView.isFullScreen) {
//            [self.playerView originalscreen];
//        }
//        else {
      [self.playerView originalscreen];
//      [self.playerView removeFromSuperview];
      [self dismissViewControllerAnimated:YES completion:nil];
//        }
    }];
    
    // 播放完成回调
    [self.playerView endPlay:^{
        NSLog(@"播放完成");
    }];
  
  
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(fullScreen)
                                               name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)fullScreen {
  if (!self.playerView.isFullScreen) {
    [self.playerView fullScreenWithDirection:UIInterfaceOrientationLandscapeLeft];
  }
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  
  [self.playerView fullScreenWithDirection:UIInterfaceOrientationLandscapeLeft];
}

- (void)viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
  //    销毁播放器
  [self.playerView pausePlay];
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (CLPlayerView *)playerView {
    if (!_playerView) {
        _playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        //            //重复播放，默认不播放
        //            _playerView.repeatPlay = YES;
        //            //当前控制器是否支持旋转，当前页面支持旋转的时候需要设置，告知播放器
        _playerView.isLandscape = NO;
        // 视频拉伸模式 按原视频比例显示，是竖屏的就显示出竖屏的，两边留黑
        _playerView.fillMode = ResizeAspect;
        // 设置进度条背景颜色
        _playerView.progressBackgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
        // 设置进度条缓冲颜色
        _playerView.progressBufferColor = [UIColor whiteColor];
        // 设置进度条播放完成颜色
      _playerView.progressPlayFinishColor = [UIColor greenColor]; // UIColorHex(0xff6f26);
        // 全屏是否隐藏状态栏
        _playerView.fullStatusBarHidden = YES;
        //    //是否静音，默认NO
        //        _playerView.mute = YES;
        //    //转子颜色
        //    _playerView.strokeColor = [UIColor redColor];
        
    }
    return _playerView;
}


@end
